<?php

namespace app\admin\model\pay;

use think\Model;
use think\Db;

class PayPasswd extends Model
{





    // 表名
    protected $table = 'pay_passwd';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_must_text'
    ];



    public function getIsMustList()
    {
        return ['1' => __('Is_must 1'), '2' => __('Is_must 2')];
    }


    public function getIsMustTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_must']) ? $data['is_must'] : '');
        $list = $this->getIsMustList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public static function confirm($user_id,$pwd)
    {
        Db::startTrans();
        $user = self::where('user_id','=',$user_id)->lock(true)->find();
        $res = ['code'=>0,'msg'=>__('您还未设置支付密码，请先设置您的支付密码。')];
        if (!$user) {
            Db::commit();
            return $res;
        }
        /*if ($user->confirmtime > time() - 10) {
            $res['msg'] = __('您的操作太快了！');
            Db::commit();
            return $res;
        }*/

        $pwd = call_user_func($user['type'],$pwd);
        $num = 4;
        if ($pwd != $user['passwd']) {
            $res['msg'] = __('支付密码不正确');// 第一次
            if ($user->err_num > 0 && $num >= $user->err_num ) { // 不是第一次 并错误次数未超过
                $res['msg'] = sprintf(__('支付密码不正确，您还可以输入%s次'),$num-$user->err_num );
            }elseif ($num < $user->err_num && time()-10800 > $user->confirmtime) {// 超过3小时
                $user->err_num = 0;
            }elseif ($num < $user->err_num) {
                $res['msg'] = __('支付密码输入错误次数过多，请3个小时后再进行设置');
            }
            $user->confirmtime = time();
            $user->err_num +=1;
            $user->save();
            Db::commit();
            return $res;
        }
        $user->confirmtime = time();
        $user->err_num = 0;
        $user->save();
        Db::commit();
        return ['code'=>1];
    }
    public static function edit($user_id,$pwd,$new_pwd)
    {
        $res = ['code'=>0,'msg'=>__('您还未设置支付密码，请先设置您的支付密码。')];
        if (!$new_pwd) {
            $res['msg'] = __('密码不能为空');
            return $res;
        }
        $user = self::where('user_id','=',$user_id)->find();
        if (!$user) {
            return $res;
        }
        $pwd = call_user_func($user['type'],$pwd);
        if ($pwd != $user['passwd']) {
            $res['msg'] = __('旧密码不正确，请重新输入');
            return $res;
        }
        if(md5($new_pwd) == $user['passwd']){
            $res['msg'] = __('新密码不能和旧密码相同');
            return $res;
        }
        $user->passwd = md5($new_pwd);
        $user->save();
        return ['code'=>1];
    }
    const IS_MUST_2 = '2';
    const IS_MUST_1 = '1';
    public static function nopwd($user_id,$is_must)
    {
        $user = self::where('user_id','=',$user_id)->find();
        $res = ['code'=>0,'msg'=>__('您还未设置支付密码，请先设置您的支付密码。')];
        if (!$user) {
            return $res;
        }
        if ($is_must=='2') {
            $user->is_must = self::IS_MUST_2;
        }else{
            $user->is_must = self::IS_MUST_1;
        }
        return ['code'=>1];
    }
    public static function info($user_id)
    {
        $user = self::where('user_id','=',$user_id)->find();
        $res = ['code'=>0,'msg'=>__('您还未设置支付密码，请先设置您的支付密码。')];
        if (!$user) {
            return $res;
        }
        return ['code'=>1];
    }
    /**
     * [setPwd 设置支付密码]
     * @since 2022-11-11
     * @param [type]     $user_id [description]
     * @param [type]     $pwd     [description]
     * @param [type]     $code    [description]
     */
    public static function setPwd($user_id,$pwd,$code='')
    {
        $res = ['code'=>0,'msg'=>__('用户不存在')];
        if (!$pwd) {
            $res['msg'] = __('密码不能为空');
            return $res;
        }
        $userMod = new \app\admin\model\User;
        $user = $userMod->field('mobile')->where('id','=',$user_id)->find();

        if (!$user) {
            return $res;
        }
        if ($code) {
            $check = \app\common\library\Sms::check($user->mobile, $code, $event = 'resetpwd');
            if (!$check) {
                $res['msg'] = __('验证码不正确');
                return $res;
            }
        }

        $user = self::where('user_id','=',$user_id)->find();
        if (!$user) {
            $user = new self;
            $user->user_id = $user_id;
        }
        $user->type = 'md5';
        // $user->confirmtime = time();
        $user->err_num = 0;
        $user->passwd = md5($pwd);
        $user->save();
        return ['code'=>1];
    }
}
